package com.wanpinghui.supplier.user.company;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.widgets.TimeSelectorDialog;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.model.user.bean.CaseBean;
import com.wanpinghui.wphlibrary.photovideo.bean.UploadFileBean;
import com.wanpinghui.wphlibrary.photovideo.view.ClipActivity;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.utils.TimeUtil;
import com.yunshl.yunshllibrary.view.YunShlEditText;

import org.xutils.common.util.DensityUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import me.nereo.multi_image_selector.PhotoPickManager;

/**
 * Created by tg596 on 2018/1/25.
 */
@ContentView(R.layout.layou_activity_add_case)
public class AddCaseActivity extends BaseActivity {

    private static final int REQUEST_SELECT_PHOTO = 0x01;
    private static final int CLIP_PHOTO = 0x12;
    private ArrayList<UploadFileBean> userList;

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser tpl_title;

    @ViewInject(R.id.tv_registered_time)
    private TextView mTvRegisteredTime;

    @ViewInject(R.id.iv_company_logo)
    private ImageView iv_company_logo;

    @ViewInject(R.id.edt_product_number)
    private YunShlEditText edt_product_number;

    @ViewInject(R.id.edt_area)
    private YunShlEditText edt_area;

    @ViewInject(R.id.tv_upload_img)
    private TextView tv_upload_img;

    @ViewInject(R.id.fl_upload_img)
    private FrameLayout fl_upload_img;

    @ViewInject(R.id.edt_case_name)
    private YunShlEditText edt_case_name;

    private TimeSelectorDialog dialog;

    private long case_date;

    private CaseBean bean;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        bean = getIntent().getParcelableExtra("bean");
        fl_upload_img.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.getScreenWidth()*35/75));
        if (bean != null){
            edt_area.setText(bean.getCase_area());
            edt_case_name.setText(bean.getCase_name());
            edt_product_number.setText(bean.getCase_type());
            case_date = bean.getCase_date()*1000;
            mTvRegisteredTime.setText(TimeUtil.format(case_date, "yyyy-MM-dd"));
            if (TextUtil.isNotEmpty(bean.getPictures())){
                tv_upload_img.setVisibility(View.INVISIBLE);
                Glide.with(getApplicationContext()).load(bean.getPictures()).fitCenter().into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        fl_upload_img.setBackground(resource);
                    }
                });
            }
        }
    }

    @Override
    public void bindEvents() {
        fl_upload_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoPickManager.selectPic(AddCaseActivity.this, REQUEST_SELECT_PHOTO, 1, false, 0);
            }
        });
        tpl_title.setOnClickRightTxt(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CaseBean bean = new CaseBean();
                if (userList!= null && userList.size()>0){

                bean.setPictures(userList.get(0).getPath());

                }
                bean.setCase_name(edt_case_name.getTextString());
                bean.setCase_type(edt_product_number.getTextString());
                bean.setCase_area(edt_area.getTextString());
                bean.setCase_date(case_date/1000);
                Intent intent = new Intent();
                intent.putExtra("bean",bean);
                setResult(RESULT_OK,intent);
                finish();

            }
        });
        mTvRegisteredTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog == null) {
                    dialog = new TimeSelectorDialog(AddCaseActivity.this);
                    dialog.setBindClickListener(new TimeSelectorDialog.BindClickListener() {
                        @Override
                        public void selectTime(long startTime ) {
                            case_date = startTime;
                            if (startTime > System.currentTimeMillis()){
                                showWarningText("所选日期不能大于当前日期");
                            }else {
                                mTvRegisteredTime.setText(TimeUtil.format(startTime, "yyyy-MM-dd"));
                            }
                        }
                    });
                }
                dialog.setTime(mTvRegisteredTime.getText().toString());
                dialog.show();
            }
        });
        tpl_title.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void initData() {
        tpl_title.showRightText();
        tpl_title.setRightText("确认");
        tpl_title.setRightTextColor(ContextCompat.getColor(this,R.color.colorPrimaryBlue));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_PHOTO) {
            if (data != null) {
                Intent intent = new Intent(AddCaseActivity.this, ClipActivity.class);
                intent.putExtra("data", PhotoPickManager.getPhoto(data));
                intent.putExtra("scale",4);
                startActivityForResult(intent, CLIP_PHOTO);
            }
        } else if (requestCode == CLIP_PHOTO) {
            if (data != null) {
                //        LoadingDialog.Build(this).setContent("正在上传").show();
                ArrayList list = data.getParcelableArrayListExtra("result");
                userList = new ArrayList<>();
                userList.addAll(list);
                final String paths = userList.get(0).getPath();
                tv_upload_img.setVisibility(View.INVISIBLE);
                Glide.with(getApplicationContext()).load(paths).fitCenter().into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        fl_upload_img.setBackground(resource);
                    }
                });

            }
        }
    }

}
